package com.gcxy.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author lichuandong
 * @Date 2022/2/16
 */
@Slf4j
public class SecurityUtil {


    public static String getTokenFromRequest(HttpServletRequest request) {
        String authorization = request.getHeader("Authorization");
        log.debug("Authorization is-------->>>>>{}", authorization);
        return authorization;
    }

    public static boolean allowedUriCheck(String uriPattern, String requestURI) {
        AntPathMatcher antPathMatcher = new AntPathMatcher();
        boolean isAllowedPass = antPathMatcher.match(uriPattern, requestURI);
        if (isAllowedPass) {
            log.debug("allowedUriCheck allowed list contain uri do nothing--------{}>>>>>{}", uriPattern, requestURI);
            return true;
        }
        return false;

    }

    public static boolean isAllowedListContains(String requestUri,List<String> whiteList) {
        return CollectionUtils.isEmpty(whiteList) ? false : whiteList.stream().anyMatch((it) -> {
            return allowedUriCheck(it, requestUri);
        });
    }
}
